package com.example.controller;
import com.example.Service.HealthRecordService;
import com.example.entity.HealthRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/health")
@CrossOrigin(origins = "*")
public class HealthRecordController {

    @Autowired
    private HealthRecordService healthRecordService;

    @PostMapping("/add")
    public String addRecord(@RequestBody HealthRecord record) {
        return healthRecordService.addHealthRecord(record) > 0 ? "success" : "fail";
    }

    @DeleteMapping("/delete/{recordId}")
    public String deleteRecord(@PathVariable Integer recordId) {
        return healthRecordService.deleteHealthRecordById(recordId) > 0 ? "success" : "fail";
    }

    @PutMapping("/update")
    public String updateRecord(@RequestBody HealthRecord record) {
        return healthRecordService.updateHealthRecord(record) > 0 ? "success" : "fail";
    }

    @GetMapping("/user/{userId}")
    public List<HealthRecord> getRecordsByUserId(@PathVariable Integer userId) {
        return healthRecordService.getHealthRecordsByUserId(userId);
    }
}
